//
//  GRHaveBeenShopViewController.m
//  YC_GRice
//
//  Created by Feng Liu on 2018/8/30.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRHaveBeenShopViewController.h"
#import "GRStoreViewController.h"
#import "GRPayBillViewController.h"

#import "GRCollectionShopViewCell.h"

@interface GRHaveBeenShopViewController ()
{
    CLLocationCoordinate2D coordinate;
    CGFloat  shopHeight;
}
@property (nonatomic, strong) NSMutableArray *shopList;

@end

@implementation GRHaveBeenShopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.commontitle = @"去过的店";
    
    self.blankSpace.contentMode = GRBlankContentModeEmptyBeenShop;
    // Do any additional setup after loading the view.
    [self.tableView registerNib:[UINib nibWithNibName:@"GRCollectionShopViewCell" bundle:nil] forCellReuseIdentifier:@"GRCollectionShopViewCell"];
    shopHeight = 22 / 75.0 * KscreenWidth;
    coordinate = [GRLocationManager defaultManager].currentLocation;
    [self getHaveBeenToShop];
    [self MJRefresh];
}

/**
 获取去过的店
 */
static NSInteger pageIndex = 1;
- (void)getHaveBeenToShop {
    NSDictionary *paramer = @{@"lantitude" : @(coordinate.latitude),
                                          @"longitude" : @(coordinate.longitude),
                                          @"pageIndex" : @(pageIndex),
                                          @"pageSize" : @10
                                          };
    WEAKSELF
    [GRUserAction getHaveBeenToShopWithParamer:paramer completionHandler:^(BOOL success, id responseObject, GRError *error) {
        if (success) {
            if (pageIndex == 1) {
                [weakSelf.shopList removeAllObjects];
            }
            [weakSelf.shopList addObjectsFromArray:responseObject];
            weakSelf.blankSpace.hidden = weakSelf.shopList.count == 0 ? NO : YES;
            [weakSelf.tableView reloadData];
        }else {
            [error errorAlertWithUIView:weakSelf.view offsetY:0 completion:^{
                [weakSelf getHaveBeenToShop];
            }];
        }
        
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}

- (void)MJRefresh {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        pageIndex = 1;
        [self getHaveBeenToShop];
    }];
    
    self.tableView.mj_footer = [GRRefreshBackFooter footerWithRefreshingBlock:^{
        pageIndex += 1;
        [self getHaveBeenToShop];
    }];
}

#pragma Mark ----------  <UITableViewDelegate, UITableViewDataSource>  -------------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.shopList.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GRCollectionShopViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GRCollectionShopViewCell" forIndexPath:indexPath];
    cell.moreBtn.hidden                = YES;
    cell.beenShopModel                = self.shopList[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return shopHeight;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    GRCollectCommonModel *model    = self.shopList[indexPath.row];
    if ([GRUserBasisInfo isLogin]) {
        GRPayBillViewController *billVc   = [[GRPayBillViewController alloc] init];
        billVc.storeId                            = model.profileOnePayId;
        //    GRStoreViewController *storeVc = [[GRStoreViewController alloc] init];
        //    storeVc.storeId                        = model.profileOnePayId;
        billVc.isValid = YES;
        [self.navigationController pushViewController:billVc animated:YES];
    }else{
        [self goToLogin];
 
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSMutableArray *)shopList {
    if (!_shopList) {
        _shopList = [NSMutableArray array];
    }
    return _shopList;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
